#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity.h"
#include "mock_bsp_test_commom.h"  // 假设这是包含rt_ATRWrite和rt_ATCRead声明的头文件

#include "rt_AcqTrans_proc.h"


// 测试用例变量
static XRt1553B testInstance;
static XRt1553B_MsgBlkDesc testCMDInfo;
static B1553_BusService testBusServ;


// 测试初始化函数
void setUp(void)
{
    // 初始化测试数据
    memset(&testInstance, 0, sizeof(XRt1553B));
    memset(&testCMDInfo, 0, sizeof(XRt1553B_MsgBlkDesc));
    memset(&testBusServ, 0, sizeof(B1553_BusService));
    
    // 重置Mock调用计数
    // 这里假设使用Unity的Mock框架
}

void tearDown(void)
{
    // 测试清理
}

// 测试用例1: 发送方向不处理
void test_rt_DistrbTrans_proc_Transmit_Direction(void)
{
    // Arrange
    testCMDInfo.T_or_R = 1; // 发送方向
    
    // Act
    rt_DistrbTrans_proc(&testInstance, &testCMDInfo, &testBusServ);
    
    // Assert
    // 函数应该直接返回，不修改任何数据
    TEST_ASSERT_EQUAL(0, testBusServ.Msg_Rx_cnt);
    // 可以验证Mock函数未被调用（如果框架支持）
}

// 测试用例2: DTD复位处理
void test_rt_DistrbTrans_proc_DTD_Reset(void)
{
    // Arrange
    testCMDInfo.T_or_R = 0; // 接收方向
    testBusServ.DistrbTransDesc_ctrl = CCSDS_DTD_RESET_MASK | 0x05; // 包含复位标记
    
    // Expect rt_DTDRead to be called
    rt_DTDRead_Expect(&testInstance, &testBusServ);
    
    // Expect rt_DTCWrite to be called
    rt_DTCWrite_Expect(&testInstance, &testBusServ);
    
    // Act
    rt_DistrbTrans_proc(&testInstance, &testCMDInfo, &testBusServ);
    
    // Assert
    TEST_ASSERT_EQUAL(0, testBusServ.DistrbTransCfm_bytes);
    TEST_ASSERT_EQUAL(CCSDS_DTC_RESET, testBusServ.DistrbTransCfm_ctrl);
    TEST_ASSERT_EQUAL(0, testBusServ.Msg_Rx_cnt); // 复位时不增加接收计数
}

// 测试用例3: 无复位且块计数相同（无新数据）
void test_rt_DistrbTrans_proc_No_New_Data(void)
{
    // Arrange
    testCMDInfo.T_or_R = 0; // 接收方向
    testBusServ.DistrbTransDesc_ctrl = 0x05; // 无复位标记
    testBusServ.DistrbTransCfm_ctrl = 0x05;  // 相同的块计数
    testBusServ.DistrbTransDesc_bytes = 100;
    testBusServ.DistrbTransCfm_bytes = 50;
    testBusServ.Msg_Rx_cnt = 10;
    
    // Expect rt_DTDRead to be called
    rt_DTDRead_Expect(&testInstance, &testBusServ);
    
    // Act
    rt_DistrbTrans_proc(&testInstance, &testCMDInfo, &testBusServ);
    
    // Assert
    // 应该不更新数据，因为没有新数据
    TEST_ASSERT_EQUAL(50, testBusServ.DistrbTransCfm_bytes); // 保持原值
    TEST_ASSERT_EQUAL(10, testBusServ.Msg_Rx_cnt); // 接收计数不变
    // rt_DTCWrite不应该被调用
}

// 测试用例4: 无复位且块计数不同（有新数据）
void test_rt_DistrbTrans_proc_Has_New_Data(void)
{
    // Arrange
    testCMDInfo.T_or_R = 0; // 接收方向
    testBusServ.DistrbTransDesc_ctrl = 0x06; // 无复位标记，块计数为6
    testBusServ.DistrbTransCfm_ctrl = 0x05;  // 块计数为5，不同
    testBusServ.DistrbTransDesc_bytes = 200;
    testBusServ.DistrbTransCfm_bytes = 100;
    testBusServ.Msg_Rx_cnt = 5;
    
    // Expect rt_DTDRead to be called
    rt_DTDRead_Expect(&testInstance, &testBusServ);
    
    // Expect rt_DTCWrite to be called
    rt_DTCWrite_Expect(&testInstance, &testBusServ);
    
    // Act
    rt_DistrbTrans_proc(&testInstance, &testCMDInfo, &testBusServ);
    
    // Assert
    TEST_ASSERT_EQUAL(200, testBusServ.DistrbTransCfm_bytes); // 应该更新为新值
    TEST_ASSERT_EQUAL(0x06, testBusServ.DistrbTransCfm_ctrl); // 应该更新为新值
    TEST_ASSERT_EQUAL(6, testBusServ.Msg_Rx_cnt); // 接收计数应该增加1
}

// 测试用例5: 边界条件测试 - 块计数掩码边界值
void test_rt_DistrbTrans_proc_Block_Count_Boundary(void)
{
    // Arrange
    testCMDInfo.T_or_R = 0; // 接收方向
    testBusServ.DistrbTransDesc_ctrl = CCSDS_DTD_BLOCK_CNT_MASK; // 最大块计数值
    testBusServ.DistrbTransCfm_ctrl = 0x00;  // 块计数为0
    testBusServ.Msg_Rx_cnt = 0;
    
    // Expect rt_DTDRead to be called
    rt_DTDRead_Expect(&testInstance, &testBusServ);
    
    // Expect rt_DTCWrite to be called
    rt_DTCWrite_Expect(&testInstance, &testBusServ);
    
    // Act
    rt_DistrbTrans_proc(&testInstance, &testCMDInfo, &testBusServ);
    
    // Assert
    TEST_ASSERT_EQUAL(1, testBusServ.Msg_Rx_cnt); // 接收计数应该增加1
    TEST_ASSERT_EQUAL(CCSDS_DTD_BLOCK_CNT_MASK, testBusServ.DistrbTransCfm_ctrl);
}

// 测试用例6: 复位标记和块计数同时存在
void test_rt_DistrbTrans_proc_Reset_With_Block_Count(void)
{
    // Arrange
    testCMDInfo.T_or_R = 0; // 接收方向
    // 复位标记和块计数同时存在，复位优先级更高
    testBusServ.DistrbTransDesc_ctrl = CCSDS_DTD_RESET_MASK | CCSDS_DTD_BLOCK_CNT_MASK;
    testBusServ.DistrbTransCfm_ctrl = 0x05;
    testBusServ.Msg_Rx_cnt = 3;
    
    // Expect rt_DTDRead to be called
    rt_DTDRead_Expect(&testInstance, &testBusServ);
    
    // Expect rt_DTCWrite to be called
    rt_DTCWrite_Expect(&testInstance, &testBusServ);
    
    // Act
    rt_DistrbTrans_proc(&testInstance, &testCMDInfo, &testBusServ);
    
    // Assert - 复位逻辑应该执行
    TEST_ASSERT_EQUAL(0, testBusServ.DistrbTransCfm_bytes);
    TEST_ASSERT_EQUAL(CCSDS_DTC_RESET, testBusServ.DistrbTransCfm_ctrl);
    TEST_ASSERT_EQUAL(3, testBusServ.Msg_Rx_cnt); // 复位时不增加接收计数
}